package com.jdew.thkj.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.adapter.TixianAdapter;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.bean.TiXianDetailBean;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TiXianDetailActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.recycleView)
    RecyclerView recycleView;
    @Bind(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @Bind(R.id.message)
    TextView message;
    @Bind(R.id.none_view)
    RelativeLayout noneView;
    @Bind(R.id.time)
    TextView time;
    @Bind(R.id.money)
    TextView money;
    private int page = 1;
    private TixianAdapter adapter;
    private List<TiXianDetailBean.DataBean> list = new ArrayList<>();
    private boolean isLoading;
    private int totalCount;

    @Override
    public int bindLayout() {
        return R.layout.activity_tixian_detail;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        title.setText("提现记录");
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        adapter = new TixianAdapter(this, list);
        setRcycleview(recycleView, adapter);
        back.setOnClickListener(this);
        setLoadMore();
        RequestData();
        initRefresh();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            default:
                break;
        }
    }

    private void initRefresh() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                RequestData();
                swipeRefreshLayout.setRefreshing(false);
            }
        });
    }

    public void RequestData() {
        String timesamp = RegexValidateUtils.getTime13();
        String numsamp = RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL).createReq(RequestApi.class).GetPresentRecordNew(sp.getString("userPhone", ""), page, RegexValidateUtils.getUniquePsuedoID(), timesamp, numsamp, MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp + numsamp + RegexValidateUtils.getUniquePsuedoID() + sp.getString("SignToken", "") + "UserPhone" + sp.getString("userPhone", "") + "PageIndex" + page)).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<TiXianDetailBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(TiXianDetailBean bean) {
                if (bean.getCode().equals("0")) {
                    totalCount = Integer.parseInt(bean.getMessage());
                    if (null == bean.getData()) {
                        adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
                        isLoading = false;
                        swipeRefreshLayout.setRefreshing(false);
                        adapter.notifyItemRemoved(adapter.getItemCount());
                        return;
                    }
                    time.setText(bean.getNumto());
                    money.setText(bean.getNum());
                    SpannableString spannableString= new SpannableString(bean.getNum());
                    RelativeSizeSpan sizeSpan = new RelativeSizeSpan(1.2f);
                    spannableString.setSpan(sizeSpan, 0, bean.getNum().toString().indexOf("."), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                    money.setText(spannableString);
                    if (page == 1) {
                        list.clear();
                    }
                    list.addAll(bean.getData());
                    if (list.size() == totalCount) {
                        adapter.changeMoreStatus(adapter.NO_LOAD_MORE);
                    } else {
                        adapter.changeMoreStatus(adapter.PULL_LOAD);
                    }
                    adapter.setDataList(list);
                    isLoading = false;
                    swipeRefreshLayout.setRefreshing(false);
                    adapter.notifyItemRemoved(adapter.getItemCount());
                } else {
                    if (totalCount == 0) {
                        recycleView.setVisibility(View.GONE);
                        noneView.setVisibility(View.VISIBLE);
                        message.setText("您还没有提现记录哦");
                        time.setText(bean.getNumto());
                        money.setText(bean.getNum());
                    }
                }
            }
        });
    }

    public void setLoadMore() {
        recycleView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                LinearLayoutManager linearManager = null;
                if (layoutManager instanceof LinearLayoutManager) {
                    linearManager = (LinearLayoutManager) layoutManager;
                }
                int lastVisibleItemPosition = linearManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                    boolean isRefreshing = swipeRefreshLayout.isRefreshing();
                    if (isRefreshing) {
                        adapter.notifyItemRemoved(adapter.getItemCount());
                        return;
                    }
                    if (!isLoading) {
                        isLoading = true;
                        page++;
                        RequestData();
                    }
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
}
